package com.simba.server.commands.impl.fetch;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import com.simba.common.command.ICommand;
import com.simba.common.database.DbOperator;

public class FetchTrashSlotsCmd implements ICommand
{
	public static final String NAME = "FetchTrashSlotsCmd";
	private final String sql = "select * from tt_trash_slots;";
	private final Map<Integer, List<Integer>> trashSlotInfos;

	public FetchTrashSlotsCmd(Map<Integer, List<Integer>> trashSlotInfos)
	{
		this.trashSlotInfos = trashSlotInfos;
	}

	@Override
	public String getName()
	{
		return FetchTrashSlotsCmd.NAME;
	}

	@Override
	public Boolean run(Object params)
	{
		Boolean ret = false;

		if (!(params instanceof DbOperator))
		{
			return ret;
		}

		try
		{
			DbOperator dbOperator = (DbOperator) params;

			dbOperator.prepareStatement(this.sql);

			ResultSet rs = dbOperator.executeQuery();

			while (rs.next())
			{
				int slotId = rs.getInt("slotId");
				int typeId = rs.getInt("typeId");
				if (!this.trashSlotInfos.containsKey(typeId))
				{
					this.trashSlotInfos.put(typeId, new ArrayList<>());
				}

				this.trashSlotInfos.get(typeId).add(slotId);
			}

			ret = true;
		}
		catch (Exception ex)
		{
			ret = false;
		}

		return ret;
	}

}
